package com.plian.system.service.sys.orgzation.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.system.constant.sys.OrgConstant;
import com.plian.system.constant.sys.SystemConstant;
import com.plian.system.entity.sys.BaseServiceOrg;
import com.plian.system.entity.sys.Org;
import com.plian.system.entity.sys.ServiceOrg;
import com.plian.system.entity.sys.ServiceOrgFormType;
import com.plian.system.mapper.sys.BaseServiceOrgMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.sys.orgzation.IBaseServiceOrgService;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.sys.orgzation.IServiceOrgFormTypeService;
import com.plian.system.service.sys.orgzation.IServiceOrgService;
import com.plian.system.wrapper.sys.ServiceOrgWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.plian.system.constant.sys.OrgConstant.ORG_DEFAULT_DATE;


@Slf4j
@Service
@AllArgsConstructor
public class BaseServiceOrgServiceImpl extends BaseServiceImpl<BaseServiceOrgMapper, BaseServiceOrg> implements IBaseServiceOrgService {

    private IServiceOrgService serviceOrgService;

    private IServiceOrgFormTypeService serviceOrgFormTypeService;

    private IOrgService orgService;

    private ServiceOrgWrapper serviceOrgWrapper;

    @Override
    public boolean deleteLogic(List<Long> ids) {
        boolean result = super.deleteLogic(ids);
        if (result){
           serviceOrgService.update(Wrappers.<ServiceOrg>lambdaUpdate()
                   .in(ServiceOrg::getBaseServiceOrgId, ids).set(ServiceOrg::getIsDel, OrgConstant.IS_DEL));
            serviceOrgFormTypeService.update(Wrappers.<ServiceOrgFormType>lambdaUpdate()
                    .in(ServiceOrgFormType::getBaseServiceOrgId, ids).set(ServiceOrgFormType::getBaseServiceOrgId, null));
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean submit(BaseServiceOrg baseServiceOrg) {
        if (baseServiceOrg.getId() == null){
            if (baseServiceOrg.getIsEnabled() == null){
                baseServiceOrg.setIsEnabled(SystemConstant.YES);
            }
            saveOrUpdate(baseServiceOrg);
            return serviceOrgService.copyOtherServiceOrg(baseServiceOrg.getCopyId(), baseServiceOrg.getId());
        }else {
            return saveOrUpdate(baseServiceOrg);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean initOrg() {
        BaseServiceOrg baseServiceOrg = new BaseServiceOrg();
        baseServiceOrg.setServiceOrgName("主版本");
        baseServiceOrg.setIsMain(SystemConstant.YES);
        baseServiceOrg.setIsEnabled(SystemConstant.YES);
        baseServiceOrg.setOrderNumber(0);
        save(baseServiceOrg);
        List<Org> orgList = orgService.getAllOrg();
        List<ServiceOrg> serviceOrgList = serviceOrgWrapper.orgToServiceOrg(orgList);
        for (ServiceOrg serviceOrg : serviceOrgList){
            serviceOrg.setBaseServiceOrgId(baseServiceOrg.getId());
            serviceOrg.setInitialId(serviceOrg.getId());
            serviceOrg.setMainVersionInitialId(serviceOrg.getId());
            serviceOrg.setIsLatest(SystemConstant.YES);
            serviceOrg.setVersion(1);
            serviceOrg.setIsEffective(SystemConstant.YES);
            serviceOrg.setEffectiveTime(ORG_DEFAULT_DATE);
            if (serviceOrg.getCreateTime() == null){
                serviceOrg.setCreateTime(ORG_DEFAULT_DATE);
                serviceOrg.setUpdateTime(ORG_DEFAULT_DATE);
            }
        }
        return serviceOrgService.saveBatch(serviceOrgList);
    }


}
